%!TEX program = xelatex
%!TEX TS-program = xelatex
%!TEX encoding = UTF-8 Unicode

\documentclass[12pt,t,aspectratio=169,mathserif]{beamer}
%Other possible values are: 1610, 149, 54, 43 and 32. By default, it is to 128mm by 96mm(4:3).
%run XeLaTeX to compile.

\input{wang-slides-preamble.tex}

\begin{document}

\title{第3章：Python简明教程}
%(1.1-1.2) 
%\institute{上海立信会计金融学院}
\author{JMS LQW}
%\date{2021年3月12日}

\maketitle

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%\begin{frame}[fragile=singleslide]{6.1.1. }
\begin{frame}{目录 }

\vspace{-0.4cm}\noindent\makebox[\linewidth]{\rule{\paperwidth}{0.4pt}}
%每页详细内容

\begin{enumerate}
\item[3.1.]  输入 Python 代码 
\item[3.2.]  对象与标识符
\item[3.3.]  数值类型
\item[3.4.] 名称空间和模块
\item[3.5.]  容器对象：列表、元组、字符串、字典
\item[3.6.]  Python 的 if 判断语句
\item[3.7.]  Python 的循环结构：for 语句和 while 语句
\item[3.8.]  函数：语法、作用范围、参数、输入输出函数、匿名函数
\item[3.9.]  Python 的类：例子：Fractions 类 
\item[3.10.]  Python 程序结构
\item[3.11.]  实用示例：素数

\end{enumerate}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile=singleslide]{3.1. 输入 Python 代码 }
%\begin{frame}{}

\vspace{-0.4cm}\noindent\makebox[\linewidth]{\rule{\paperwidth}{0.4pt}}
%每页详细内容

\begin{itemize}

\item  问：Python 基本上不使用小括号和大括号来表示一个代码块，而是使用什么方式?

\item  问：Python 的两种注释的方法是什么？

\begin{python}
''' This is the first line of the comment.
    This is the second line of the comment. '''

a = 1

#This is a comment.
b = 1

\end{python}

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile=singleslide]{3.2. 对象和标识符}
%\begin{frame}{}

\vspace{-0.4cm}\noindent\makebox[\linewidth]{\rule{\paperwidth}{0.4pt}}
%每页详细内容

\begin{itemize}

\item  问：什么是对象(object)？什么是标识符(identifier)？

\item  问：什么是一个对象的类型(type)？什么是一个对象的标识(identity)？

\begin{python}
p = 3.14
id(p)
type(p)

q = p
id(q)
type(q)

p = 'pi'
id(p)
type(p)
\end{python}

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%\begin{frame}[fragile=singleslide]{1.1. }
\begin{frame}{3.2. Python 中赋值的示意图}

\vspace{-0.4cm}\noindent\makebox[\linewidth]{\rule{\paperwidth}{0.4pt}}
%每页详细内容

\begin{figure}
\centering
\includegraphics[height=0.7\textheight, width=0.9\textwidth]{pic/fig-3-1-assignment.png}
% \caption{ }
\end{figure}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile=singleslide]{3.3.1. 整型}
%\begin{frame}{3.3.1. 数值类型：整型 Integers}

\vspace{-0.4cm}\noindent\makebox[\linewidth]{\rule{\paperwidth}{0.4pt}}
%每页详细内容

\begin{itemize}

\item  问：整数型数据的加减乘除运算是如何进行的？

\begin{python}
a = 5
b = 2

a + b
a - b
a * b
a / b

a // b
a % b
a ** b
\end{python}

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile=singleslide]{3.3.2. 实数 }
%\begin{frame}{}

\vspace{-0.4cm}\noindent\makebox[\linewidth]{\rule{\paperwidth}{0.4pt}}
%每页详细内容

\begin{itemize}

\item  问：如何在 Python 里表示一个实数？

\begin{python}
a = 3.14
type(a)
\end{python}

\item  问： Python 的 math 模块包含了许多用于实数的标准数学函数，如何使用这些函数？

\begin{python}
import math
dir(math)
\end{python}

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile=singleslide]{3.3.3. 布尔值  }
%\begin{frame}{}

\vspace{-0.4cm}\noindent\makebox[\linewidth]{\rule{\paperwidth}{0.4pt}}
%每页详细内容

\begin{itemize}

\item  问：布尔类型的数据有两个可能的值，分别是什么？

\begin{python}
math.tan(math.pi/4) == 1.0

x=1
y=2
z=3
u=0<=x<1<y>z
u
\end{python}

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile=singleslide]{3.3.4. 复数  }
%\begin{frame}{}

\vspace{-0.4cm}\noindent\makebox[\linewidth]{\rule{\paperwidth}{0.4pt}}
%每页详细内容

\begin{itemize}


\item  问： Python 的 cmath 模块包含了许多用于复数的标准数学函数，如何使用这些函数？

\item  问：如何同时使用 math 模块与 cmath 模块里的相同名称的函数？

\begin{python}
import cmath as cm
a=1
cm.sin(a)
\end{python}

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile=singleslide]{3.4. 名称空间和模块 }
%\begin{frame}{}

\vspace{-0.4cm}\noindent\makebox[\linewidth]{\rule{\paperwidth}{0.4pt}}
%每页详细内容

\begin{itemize}

\item  问：什么是名称空间(namespace)？

\item  当在解释器中工作时，名称空间的标识符是什么？

\item  什么是模块？如何自己定义一个模块？

\item  如何使用模块中的函数？

\begin{python}
from foo import obj1, obj2
from foo import *

from math import *
from cmath import *

import math as re
import cmath as co
\end{python}

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile=singleslide]{3.5. 容器对象  }
%\begin{frame}{}

\vspace{-0.4cm}\noindent\makebox[\linewidth]{\rule{\paperwidth}{0.4pt}}
%每页详细内容

\begin{itemize}

\item  问：大多数程序设计语言都有数组这样的容器对象。数组的主要特征是什么？

\item  问：Python 核心语言并没有数组的概念。它的基本的容器对象是哪些？

\item  问：为什么 Numpy 模块的数组具有较快的运行速度？


\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile=singleslide]{3.5.1. 列表 }
%\begin{frame}{}

\vspace{-0.4cm}\noindent\makebox[\linewidth]{\rule{\paperwidth}{0.4pt}}
%每页详细内容

\begin{itemize}

\item  问：如何创建 Python 的一个列表？

\item  问：Python 的一个列表里的元素，必须是相同类型的吗？

\begin{python}
u = [1, 4.0, 'a']
v = [3.14, 2.78, u, 42]
len(v)
len?
help(len)

v*2
v+u
v.append('foo')
\end{python}

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile=singleslide]{3.5.2. 列表索引 }
%\begin{frame}{}

\vspace{-0.4cm}\noindent\makebox[\linewidth]{\rule{\paperwidth}{0.4pt}}
%每页详细内容

\begin{itemize}

\item  问：如何访问一个列表里的元素？
\item  问：如何访问列表里指定位置的一些元素？

\begin{python}
u = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']
u[0]
u[-1]
u[2:6]
u[2:-2]
u[6:2:-1]
u[-2:-6:-1]
\end{python}

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile=singleslide]{3.5.3. 列表切片 }
%\begin{frame}{}

\vspace{-0.4cm}\noindent\makebox[\linewidth]{\rule{\paperwidth}{0.4pt}}
%每页详细内容

\begin{itemize}

\item  问：当列表的切片操作 \pyth{u[start:end]} 分别出现在赋值语句的左侧与右侧，分别发生什么？

\begin{python}
u = [0,1,2,3,4,5,6,7]
su = u[2:4]
su
su[0] = 17
su
u
u[2:4] = [3.14, 'a']
u
\end{python}

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile=singleslide]{3.5.4. 列表的可变性  }
%\begin{frame}{}

\vspace{-0.4cm}\noindent\makebox[\linewidth]{\rule{\paperwidth}{0.4pt}}
%每页详细内容

\begin{itemize}

\item  问：什么是可变对象？下述代码的结果，\pyth{u} 显示的什么？

\begin{python}
a = 4
b = a
b = 'foo'
a
b
\end{python}

\begin{python}
u = [0,1,4,9,16]
v = u
v[2] = 'foo'
v
u
\end{python}

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile=singleslide]{3.5.4.  }
%\begin{frame}{}

\vspace{-0.4cm}\noindent\makebox[\linewidth]{\rule{\paperwidth}{0.4pt}}
%每页详细内容

\begin{itemize}

\item  问：什么是列表的浅拷贝？

\begin{python}
u = [0,1,4,9,16]
v = u[ : ]
v[2] = 'foo'
v
u
\end{python}

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile=singleslide]{3.5.5. 元组 }
%\begin{frame}{}

\vspace{-0.4cm}\noindent\makebox[\linewidth]{\rule{\paperwidth}{0.4pt}}
%每页详细内容

\begin{itemize}

\item  问：元组与列表的区别是什么？

\item  问：什么是不可变对象？

\item  问：元组的最重要的用途可能是什么？

\begin{python}
(a,b,c,d) = (4, 5.0, 1.5+2j, 'a')
a,b,c,d = 4, 5.0, 1.5+2j, 'a'

foo = 1
(foo)
(foo,)
\end{python}

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile=singleslide]{3.5.6. 字符串 }
%\begin{frame}{}

\vspace{-0.4cm}\noindent\makebox[\linewidth]{\rule{\paperwidth}{0.4pt}}
%每页详细内容

\begin{itemize}

\item  问：如何创建一个字符串？

\item  问：与字符串有关的两个转换函数 \pyth{str()} 与 \pyth{eval()} 分别是怎样使用的？

\begin{python}
L = [1,2,3,5,8,13]
ls = str(L)
ls
eval(ls) == L
\end{python}

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile=singleslide]{3.5.7. 字典  }
%\begin{frame}{}

\vspace{-0.4cm}\noindent\makebox[\linewidth]{\rule{\paperwidth}{0.4pt}}
%每页详细内容

\begin{itemize}

\item  问：如何创建一个字典对象？

\begin{python}
empty = {}
parms = {'alpha':1.3, 'beta':2.74}
parms2 = dict(alpha=1.3, beta=2.74)
parms['gamma']=0.999
parms
\end{python}

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile=singleslide]{3.6. Python 的 if 语句 }
%\begin{frame}{}

\vspace{-0.4cm}\noindent\makebox[\linewidth]{\rule{\paperwidth}{0.4pt}}
%每页详细内容

\begin{itemize}

\item  问：if 语句的语法格式是怎样的？

\begin{python}
x = 0.47
if 0<x<1:
    print('x lies between zero and one.')
y = 4
\end{python}

\item  问：在 if 语句中，空语句 \pyth{pass} 是用来做什么的？

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile=singleslide]{3.7. 循环结构 }
%\begin{frame}{}

\vspace{-0.4cm}\noindent\makebox[\linewidth]{\rule{\paperwidth}{0.4pt}}
%每页详细内容

\begin{itemize}

\item  问：Python 里有哪两种循环结构？


\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile=singleslide]{3.7.1. Python 的 for 循环结构 }
%\begin{frame}{}

\vspace{-0.4cm}\noindent\makebox[\linewidth]{\rule{\paperwidth}{0.4pt}}
%每页详细内容

\begin{itemize}

\item  问：for 循环语句的格式是怎样的？

\begin{python}
c = 4
for c in 'Python':
    print(c)
c
\end{python}

\begin{python}
L = [1,4,9,16,25,36]
for it in range(len(L)):
    L[it]+=1
L
\end{python}

\begin{python}
for it in range(4):
    it*=2
    print(it)
it
\end{python}

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile=singleslide]{3.7.2. Python 的 continue 语句 }
%\begin{frame}{}

\vspace{-0.4cm}\noindent\makebox[\linewidth]{\rule{\paperwidth}{0.4pt}}
%每页详细内容

\begin{itemize}

\item  问：下述语法格式中的 \pyth{continue} 语句的效果是什么？

\begin{python}
for <iterator> in <iterable>:
    <block1>
    if <test1>:
        continue
    <block2>
<block5>
\end{python}

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile=singleslide]{3.7.3. Python 的 break 语句 }
%\begin{frame}{}

\vspace{-0.4cm}\noindent\makebox[\linewidth]{\rule{\paperwidth}{0.4pt}}
%每页详细内容

\begin{itemize}

\item  问：下述语法格式中的 \pyth{break} 语句的效果是什么？

\begin{python}
for <iterator> in <terable>:
    <block1>
    if <test2>:
        break
    <block2>
else:
    <block4>
<block5>
\end{python}

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile=singleslide]{3.7.3.  }
%\begin{frame}{}

\vspace{-0.4cm}\noindent\makebox[\linewidth]{\rule{\paperwidth}{0.4pt}}
%每页详细内容

\begin{itemize}

\item  问：下述程序的运行结果是什么？

\begin{python}
y = 107
for x in range(2,y):
    if y%x == 0:
    print(y,'has factor ', x)
    break
else:
    print(y, 'is a prime.')
\end{python}

\end{itemize}

\end{frame}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile=singleslide]{3.7.4. 列表解析 }
%\begin{frame}{}

\vspace{-0.4cm}\noindent\makebox[\linewidth]{\rule{\paperwidth}{0.4pt}}
%每页详细内容

\begin{itemize}

\item  问：什么是列表解析？

\begin{python}
L1 = [2,3,5,7,11,14]
L2 = [x**2 for x in L1]
L2
\end{python}

\begin{python}
L1 = [2,3,5,7,11,14]
L2 = [x*x for x in L1 if x%2]
L2
\end{python}

\begin{python}
import math
lpoints = [(1,0), (1,1), (4,3), (5,12)]
ldists = [math.sqrt(x*x+y*y) for (x,y) in lpoints]
\end{python}

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile=singleslide]{3.7.5. Python 的 while 循环 }
%\begin{frame}{}

\vspace{-0.4cm}\noindent\makebox[\linewidth]{\rule{\paperwidth}{0.4pt}}
%每页详细内容

\begin{itemize}

\item  问：while 语句的语法格式是什么？

\begin{python}
while True:
    print('Type Control-C to stop this!')
\end{python}

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile=singleslide]{3.8.1. 函数的语法和作用范围 }
%\begin{frame}{}

\vspace{-0.4cm}\noindent\makebox[\linewidth]{\rule{\paperwidth}{0.4pt}}
%每页详细内容

\begin{itemize}

\item  问：如何定义一个 Python 的函数？

\begin{python}
def <name> (<arglist>):
    ''' docstring: The purpose of this function is ... '''
    <body>
\end{python}

\item  问：什么时候使用空语句 \pyth{pass} ？

\item  问：如何理解帮助文档 \pyth{docstring} 对函数的重要性？

\item  问：如何理解一个函数的私有的名称空间？

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile=singleslide]{3.8.1.  }
%\begin{frame}{}

\vspace{-0.4cm}\noindent\makebox[\linewidth]{\rule{\paperwidth}{0.4pt}}
%每页详细内容

\begin{itemize}

\item  问：下述程序的运行结果显示什么？

\begin{python}
def add_one(x):
    ''' Take x and return x+1. '''
    x = x+1
    return x

x = 23
add_one?
add_one(0.456)
x
\end{python}

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile=singleslide]{3.8.1.  }
%\begin{frame}{}

\vspace{-0.4cm}\noindent\makebox[\linewidth]{\rule{\paperwidth}{0.4pt}}
%每页详细内容

\begin{itemize}

\item  问：定义一个函数，如何通过元组返回多个值？

\begin{python}
def add_x_and_y(x,y)
    ''' Add x and y and return them and their sum. '''
    z = x+y
    return x,y,z
    
a,b,c = add_x_and_y(1,0.456)
a,b,c
\end{python}

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile=singleslide]{3.8.1.  }
%\begin{frame}{}

\vspace{-0.4cm}\noindent\makebox[\linewidth]{\rule{\paperwidth}{0.4pt}}
%每页详细内容

\begin{itemize}

\item  问：当函数的参数是可变的容器如列表的时候，会有什么副作用？通过下面的例子来解释。

\begin{python}
L = [0,1,2]
print(L)
print(id(L))
def add_with_side_effects(M):
    ''' Increment the first element of the list. '''
    M[0] += 1
    
add_with_side_effects(L)
print(L)
print(id(L))
\end{python}

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile=singleslide]{3.8.2. 位置参数 }
%\begin{frame}{}

\vspace{-0.4cm}\noindent\makebox[\linewidth]{\rule{\paperwidth}{0.4pt}}
%每页详细内容

\begin{itemize}

\item  问：什么是位置参数？下述两次调用函数 \pyth{foo} 的结果一样吗？

\begin{python}
def foo(a,b,c)
    <body>

y = foo(3,2,1)
z = foo(c=1,a=3,b=2)
\end{python}

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile=singleslide]{3.8.3. 关键字参数 }
%\begin{frame}{}

\vspace{-0.4cm}\noindent\makebox[\linewidth]{\rule{\paperwidth}{0.4pt}}
%每页详细内容

\begin{itemize}

\item  问：位置参数与关键字参数同时出现在函数中，哪类参数必须放在前面？

\begin{python}
def foo(a,b,c,d=21,e=4,f='a'):
    <body>
\end{python}

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile=singleslide]{3.8.4. 可变数量的位置参数 }
%\begin{frame}{}

\vspace{-0.4cm}\noindent\makebox[\linewidth]{\rule{\paperwidth}{0.4pt}}
%每页详细内容

\begin{itemize}

\item  问：如果位置参数的个数不确定，如何编写这个函数？

\begin{python}
def average(*args):
    ''' Return mean of a non-empty tuple of numbers. '''
    print(args)
    sum = 0.0
    for x in args:
        sum += x
    return(sum/len(args))

print(average(1,2,3,4))
print(average(1,2,3,4,5))
\end{python}

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile=singleslide]{3.8.5. 可变数量的关键字参数 }
%\begin{frame}{}

\vspace{-0.4cm}\noindent\makebox[\linewidth]{\rule{\paperwidth}{0.4pt}}
%每页详细内容

\begin{itemize}

\item  问：如果关键字参数的个数不确定，如何编写这个函数？下述程序的运行结果显示什么？

\begin{python}
def show(a,b,*args, **kwargs):
    print(a,b,args,kwargs)
    
show(1.3, 2.7, 3, 4.2, alpha=0.99, gamma=5.67)
\end{python}

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile=singleslide]{3.8.6. Python 的输入输出函数 }
%\begin{frame}{}

\vspace{-0.4cm}\noindent\makebox[\linewidth]{\rule{\paperwidth}{0.4pt}}
%每页详细内容

\begin{itemize}

\item  问：如何从键盘读入一个字符串？
\begin{python}
name = input('What is your name?')
print('Your name is ' + name)
\end{python}

\item  问：如何从键盘读入一个列表？下述程序的结果是一个列表吗？

\begin{python}
ilist = input('Enter an explicit list: ')
print(ilist)
print(type(ilist))
\end{python}

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile=singleslide]{3.8.7. Python 的 print 函数 }
%\begin{frame}{}

\vspace{-0.4cm}\noindent\makebox[\linewidth]{\rule{\paperwidth}{0.4pt}}
%每页详细内容

\begin{itemize}

\item  问：格式化字符串的语法是什么？

\begin{python}
it = 10
y = 2
mystr = 'After iteration %d, the solution was %f ' % (it, y)
print(mystr)
\end{python}

\item  问：格式化语法 \pyth{%5d} 和 \pyth{%10.3f} 的含义是什么？


\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile=singleslide]{3.8.8. 匿名函数 }
%\begin{frame}{}

\vspace{-0.4cm}\noindent\makebox[\linewidth]{\rule{\paperwidth}{0.4pt}}
%每页详细内容

\begin{itemize}

\item  问：如何定义一个匿名函数？

\begin{python}
f=lambda x,y: 'yes' if x==y else 'no'
print(f(0,1))
print(f(1,1))

g=lambda x,y: (x,y,x+y)
print(g(1,2))
\end{python}

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile=singleslide]{3.9. Python 类介绍 }
%\begin{frame}{}

\vspace{-0.4cm}\noindent\makebox[\linewidth]{\rule{\paperwidth}{0.4pt}}
%每页详细内容

\begin{itemize}

\item  问：Python 的类既封装对象又封装操作，如何理解这句话？

\item  问：如何写一个类，描述‘分数’，即两个整数的商，这样的数学对象？

\item  在书本编写的 Fractions 类的基础上，增加分数的除法和减法运算。


\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile=singleslide]{3.9.  }
%\begin{frame}{}

\vspace{-0.4cm}\noindent\makebox[\linewidth]{\rule{\paperwidth}{0.4pt}}
%每页详细内容

%\begin{itemize}
%
%\item  问：下述代码做了什么？

\begin{python}
#File frac.py
#import numpy as np

class Frac:
    ''' Fractional class. A Frac is a pair of integers 
    num, den, (with den!=0) whose gcd is 1. '''
    
    def __init__(self,n,d):
        ''' Construct a Frac from intergers n and d.
        Needs error message if d=0. '''
#        hcf=np.gcd(n,d)
#        self.num, self.den = n/hcf, d/hcf
        self.num, self.den = n, d
\end{python}

%\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile=singleslide]{3.9.  }
%\begin{frame}{}

\vspace{-0.4cm}\noindent\makebox[\linewidth]{\rule{\paperwidth}{0.4pt}}
%每页详细内容


\begin{python}
    def __str__(self):
        ''' Generate a string representation of a Frac. '''
        return '%d/%d' %(self.num,self.den)
    def __mul__(self, another):
        ''' Multiply two Fracs to produce a Frac. '''
        return Frac(self.num*another.num, self.den*another.den)
    def __add__(self,another):
        ''' Add two Fracs to produce a Frac. '''
        return Frac(self.num*another.den+self.den*another.num,
                    self.den*another.den)
    def to_real(self):
        ''' Return floating point value of Frac. '''
        return float(self.num/self.den)
\end{python}


\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile=singleslide]{3.9.  }
%\begin{frame}{}

\vspace{-0.4cm}\noindent\makebox[\linewidth]{\rule{\paperwidth}{0.4pt}}
%每页详细内容

\begin{python}
if __name__ == '__main__':
    a=Frac(3,7)
    b=Frac(24,56)
    print('a.num= ',a.num,', b.den= ', b.den)
    print(a)
    print(b)
    print('floating point value of a is', a.to_real())
    print('product=', a*b, ', sum=', a+b) 
\end{python}


\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile=singleslide]{3.10. Python 程序结构 }
%\begin{frame}{}

\vspace{-0.4cm}\noindent\makebox[\linewidth]{\rule{\paperwidth}{0.4pt}}
%每页详细内容

\begin{itemize}

\item  问：什么是 Python 的点访问机制？


\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile=singleslide]{3.11. 素数：实用示例 }
%\begin{frame}{}

\vspace{-0.4cm}\noindent\makebox[\linewidth]{\rule{\paperwidth}{0.4pt}}
%每页详细内容

\begin{itemize}

\item  问：什么是 Eratosthenes 筛法？

\begin{table}[ht]
\begin{tabular}{c|c|c|c|c|c|c|c|c|c|c|c|c|c|c|c|c}
2&3&4&5&6&7&8&9&10&11&12&13&14&15&16&17&18 \\ \hline
{\color{red}2}&3&&5&&7&&9&&11&&13&&15&&17& \\ \hline
2&{\color{red}3}&&5&&7&&&&11&&13&&&&17& \\ \hline
2&3&&{\color{red}5}&&7&&&&11&&13&&&&17& \\ 
\end{tabular}
\end{table}

\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile=singleslide]{3.11.  }
%\begin{frame}{}

\vspace{-0.4cm}\noindent\makebox[\linewidth]{\rule{\paperwidth}{0.4pt}}
%每页详细内容

\begin{itemize}

\item  问：编写程序，找出1000 以内的所有素数。


\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile=singleslide]{3.11.  }
%\begin{frame}{}

\vspace{-0.4cm}\noindent\makebox[\linewidth]{\rule{\paperwidth}{0.4pt}}
%每页详细内容

\begin{python}
def sieve_v1(n):
    ''' Use Sieve of Eratosethenes to compute list of primes 
        less than or equal to n. Version 1
    '''
    primes = list(range(2,n+1))
    for p in primes:
        if p*p>n:
            break
        product = 2*p
        while product <=n:
            if product in primes:
                primes.remove(product)
            product+=p
    return len(primes), primes
\end{python}


\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile=singleslide]{3.11.  }
%\begin{frame}{}

\vspace{-0.4cm}\noindent\makebox[\linewidth]{\rule{\paperwidth}{0.4pt}}
%每页详细内容

\begin{python}
def sieve_v2(n):
    ''' Sieve of Eratosethenes to compute list of primes
        less than or equal to n. Version 2
    '''
    sieve = [True]*(n+1)
    for i in range(3,n+1,2):
        if i*i>n:
            break
        if sieve[i]:
            sieve[i*i::2*i]=[False]*((n-i*i)//(2*i)+1)
    answer = [2]+[i for i in range(3,n+1,2) if sieve[i]]
    return len(answer),answer
\end{python}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile=singleslide]{3.11. 两种算法的用时比较 }
%\begin{frame}{}

\vspace{-0.4cm}\noindent\makebox[\linewidth]{\rule{\paperwidth}{0.4pt}}
%每页详细内容

\begin{python}
from sieves import sieve_v1,sieve_v2
sieve_v1?
sieve_v1(18)
sieve_v2(18)
timeit(sieve_v1(1000))
timeit(sieve_v2(1000))
\end{python}

\begin{python}
a=list(range(10))
a[3::2]
\end{python}


\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile=singleslide]{3.11.  Who is Eratosthenes ? }
%\begin{frame}{}

\vspace{-0.4cm}\noindent\makebox[\linewidth]{\rule{\paperwidth}{0.4pt}}
%每页详细内容

\begin{itemize}

\item  John S. Conery explains the sieve of Eratosthenes in his book. % <Explorations in Computing>. 

\url{http://ix.cs.uoregon.edu/~conery/eic/}

\item  Eratosthenes (l.c. 276 - 195 BCE) was a Greek astronomer, geographer, mathematician, and poet best known for being the first to calculate the {\color{blue}circumference of the earth}
（地球周长） and its {\color{blue}axial tilt}（轨道倾角）. 

\item  He is also recognized for his mathematical innovation, the {\color{blue}Sieve of Eratosthenes}, which identified prime numbers, and his position as head of the {\color{blue}Library at Alexandria}.

\item
\url{https://www.worldhistory.org/Eratosthenes/}

\end{itemize}


\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{frame}[fragile=singleslide]{3.11.   }
%\begin{frame}{}

\vspace{-0.4cm}\noindent\makebox[\linewidth]{\rule{\paperwidth}{0.4pt}}
%每页详细内容

\begin{itemize}

\item  练习：第 1000 个素数是什么？

\item  练习：找出10000以内的间隔为2的素数对。



\end{itemize}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%\begin{frame}[fragile=singleslide]{1.20. }
\begin{frame}{参考文献}

\vspace{-0.4cm}\noindent\makebox[\linewidth]{\rule{\paperwidth}{0.4pt}}
%每页详细内容

\begin{thebibliography}{99}
\bibitem{stewart-en} John M. Stewart. \emph{Python for Scientists}. Second Edition. Cambridge University Press. 2017. 
\bibitem{stewart-cn} 约翰.M.斯图尔特(著). 江红等(译). \emph{Python科学计算}，机械工业出版社，2019年8月第1版。


\bibitem{conery} John S. Conery. \emph{Explorations in Computing, An Introduction to Computer Science and Python Programming}. CRC Press. 2015. 



\end{thebibliography}

\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\end{document}










